﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace B.S.RbacData.Infrastructure
{
    /// <summary>
    /// 仓储实现
    /// </summary>
    /// <typeparam name="T">泛型</typeparam>
    public class BaseRepository<T> : IBaseRepository<T> where T : class//泛型约束
    {

        /// <summary>
        /// 上下文
        /// </summary>
        private readonly DataDbcontext context;

        public BaseRepository(DataDbcontext context)
        {
            this.context = context;
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="entity">实体对象</param>
        /// <returns>返回受影响行数</returns>

        public async Task<int> AddAsync(T entity)
        {

            context.Set<T>().Add(entity);

            return await context.SaveChangesAsync();

        }

        /// <summary>
        /// 批量真删除
        /// </summary>
        /// <param name="ids">Id集合</param>
        /// <returns>返回实现</returns>
      
        public async Task<int> DeltelAll(List<T> ids)
        {

            context.Set<T>().RemoveRange(ids);
            return await context.SaveChangesAsync();

        }

       /// <summary>
       /// 查询所有
       /// </summary>
       /// <returns>返回查询结果</returns>
        public IQueryable<T> GetAll()
        {
          return context.Set<T>().AsQueryable();
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="entity">实体对象</param>
        /// <returns>返回受影响行数</returns>
        public async Task<int> UpdateAsync(T entity)
        {
            context.Set<T>().Update(entity);

            return await context.SaveChangesAsync();
        }

        /// <summary>
        /// 批量修改
        /// </summary>
        /// <param name="entity">实体集合</param>
        /// <returns>返回受影响行数</returns>
        public async Task<int> UpdateRanger(List<T> entity)
        {
           context.Set<T>().UpdateRange(entity);
            return await context.SaveChangesAsync();
        }
    }
}
